package battleship.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import battleship.Controller;
import battleship.PluginAI;
import battleship.Setup;

@SuppressWarnings("serial")
public class SetupGui extends JPanel{
	
	JTextField player1name;
	JTextField player2name;
	
	String[] botNames;
	
	JComboBox player1bot;
	JComboBox player2bot;
	
	String[] types;
	
	JComboBox player1type;
	JComboBox player2type;
	
	public SetupGui()
	{	
		Setup.loadAIPlugins();
		
		maakComponenten();
		addLayout();
		addListeners();
	}
	
	public void maakComponenten() {

		types  = new String[] {"bot", "human" };
		botNames = new String[Controller.getPluginsAI().size()];
		
		for(int i = 0; i < Controller.getPluginsAI().size(); i++)
		{
			botNames[i] = Controller.getPluginsAI().get(i).getName();
		}
		
		player1name = new JTextField("naam", 20);
		player2name = new JTextField("naam", 20);
		
		player1type = new JComboBox(types);
		player2type = new JComboBox(types);
		
		player1bot = new JComboBox(botNames);
		player2bot = new JComboBox(botNames);
		
		player1type.setSelectedIndex(1);
		player2type.setSelectedIndex(1);
		
		player1name.requestFocus();
		player1name.selectAll();
		
		player2name.selectAll();
		
		player1bot.setEnabled(false);
		player2bot.setEnabled(false);
	}
	
	public String getNamePlayer1()
	{
		return player1name.getText();
	}
	
	public String getNamePlayer2()
	{
		return player2name.getText();
	}
	
	public boolean getBotPlayer1()
	{	
		return (player1type.getSelectedIndex() == 0);
	}
	
	public boolean getBotPlayer2()
	{
		return (player2type.getSelectedIndex() == 0);
	}
	
	public PluginAI getAIPlayer1()
	{
		if(!getBotPlayer1()) return null;
		
		return Controller.getPluginsAI().get(player1type.getSelectedIndex());
	}
	
	
	public PluginAI getAIPlayer2()
	{
		if(!getBotPlayer2()) return null;
		
		return Controller.getPluginsAI().get(player2type.getSelectedIndex());
	}
	
	public void addLayout()
	{
		this.setLayout(new GridLayout(2, 3, 5, 5));
		
		this.add(player1name);
		this.add(player1type);
		this.add(player1bot);
		
		this.add(player2name);
		this.add(player2type);
		this.add(player2bot);
	}
	
	public void addListeners()
	{
		player1type.addActionListener(new DisableName());
		player2type.addActionListener(new DisableName());
	}
	
	class DisableName implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(player1type))
			{
				if (player1type.getSelectedIndex() == 0) // bot
				{
					player1name.setEditable(false);
					player1name.setText(Controller.getPluginsAI().get(player1bot.getSelectedIndex()).getName());
					player1bot.setEnabled(true);
					player1bot.requestFocus();
				}
				else if(player1type.getSelectedIndex() == 1) //human
				{
					player1name.setEditable(true);
					player1name.setText("naam");
					player1name.requestFocus();
					player1name.selectAll();
					player1bot.setEnabled(false);
				}
			}
			else if(e.getSource().equals(player2type))
			{
				if (player2type.getSelectedIndex() == 0) //bot
				{
					player2name.setEditable(false);
					player2name.setText(Controller.getPluginsAI().get(player2bot.getSelectedIndex()).getName());
					player2bot.setEnabled(true);
					player2bot.requestFocus();
				}
				else if (player2type.getSelectedIndex() == 1)
				{
					player2name.setEditable(true);
					player2name.setText("naam");
					player2name.requestFocus();
					player2name.selectAll();
					player2bot.setEnabled(false);
				}
			}
			
		}

	}
	
}
